package net.aaa.hotel.dao.impl;

import net.aaa.hotel.dao.HRoleDao;
import net.aaa.hotel.entity.HRole;
import net.aaa.hotel.util.BaseDB;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
@Repository
public class HRoleDaoImpl implements HRoleDao {
    @Override
    public List<Map> findAll() {
        String sql = "select * from h_role";
        return BaseDB.queryMap(sql);
    }

    @Override
    public int delRole(String gid) {
        String sql = "delete from h_role where id=?";
        return BaseDB.update(sql,gid);
    }



    @Override
    public int addRole(HRole ro) {
        if (ro.getId() == null) {
            String sql = "insert into h_role (name)values(?)";
            return BaseDB.update(sql,ro.getName());
        }else {
            String sql = "update  h_role set name=? where id=?";
            return BaseDB.update(sql,ro.getName(),ro.getId());
        }
    }

    @Override
    public List<Map> quanFen() {

        String sql = "SELECT mid AS id,mname AS title,murl,mpid FROM t_menu WHERE mpid=0";
        List<Map> maps = BaseDB.queryMap(sql);
        List<Map> digui = digui(maps);

        return digui;
    }

    @Override
    public List<Map> queryQuan(Integer rid) {
        String sql ="SELECT * FROM t_menu m RIGHT JOIN  h_role_menu r on m.mid=r.mid where  m.mpid!=0 and r.rid=?";
        return BaseDB.queryMap(sql,rid);
    }

    @Override
    public int del(int rid) {
        String sql="delete from h_role_menu where rid=?  ";


        return BaseDB.update(sql,rid);
    }

    @Override
    public int addRoleQuan(int p, int rid) {
        String sql = "insert into h_role_menu (mid,rid)values(?,?)";

        return BaseDB.update(sql,p,rid);
    }

    public  List<Map> digui(List<Map> maps){
        String sql = "SELECT mid AS id,mname AS title,murl,mpid FROM t_menu WHERE mpid=?";
        for (Map map : maps) {
            List<Map> maps1 = BaseDB.queryMap(sql, map.get("id"));
            if (maps1.size()!=0) {
                List<Map> digui = digui(maps1);
                map.put("children",digui);
            }
        }
        return maps;
    }
}
